package com.jk.controller;

import com.jk.entity.User;
import com.jk.service.HelloService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Map;

/**
 * Created by dell on 2017/6/19.
 */
@RequestMapping("http")
@RestController
public class HttpController {

    @Resource
    private HelloService helloService;

    @RequestMapping(value = "testHttpClient1", method = RequestMethod.GET)
    public User testHttpClient1(Integer id) {
        User user = helloService.selectOneUserById(id);
        return user;
    }

    @RequestMapping(value = "testHttpClient2", method = RequestMethod.POST)
    public boolean testHttpClient2(@RequestBody User user) {
        int i = 0;
        if (user.getName() != null && user.getName() != "" && user.getAge() != null && user.getAge() != 0) {
            i = helloService.insertUser(user);
            if (i > 0)
                return true;
            return false;
        } else
            return false;
    }

    @RequestMapping(value = "testHttpClient3", method = RequestMethod.PUT)
    public boolean testHttpClient3(@RequestBody User user) {
        int i = 0;
        Integer id = user.getId();
        if (id != null && id != 0) {
            i = helloService.updateUserById(user);
            if (i > 0)
                return true;
            return false;
        } else
            return false;
    }

    @RequestMapping(value = "testHttpClient4", method = RequestMethod.DELETE)
    public boolean testHttpClient4(@RequestBody Map<String, Integer> map) {
        int i = 0;
        Integer id = map.get("id");
        if (id != null && id != 0) {
            i = helloService.deleteUserById(id);
            if (i > 0)
                return true;
            return false;
        } else
            return false;
    }


}
